#include "MultiLayout.h"
#include "ImagePanel.h"

MultiLayout::MultiLayout(int iRow, int iColumn, QWidget *parent) :
	IImagePanel(parent), m_iSelectedIndex(-1), m_posMaxWidget(-1, -1), m_gridSize(iRow, iColumn)
{
	m_pLayout = new QGridLayout(this);

	for (int column = 0; column < m_gridSize.height(); ++column)
	{
		for (int row = 0; row < m_gridSize.width(); ++row)
		{
			ImagePanel *pPanel = new ImagePanel(this);

			connect(pPanel, SIGNAL(SignalSelected()), SLOT(SlotChildSelected()));
			connect(pPanel, SIGNAL(SignalWindowingValue(int,int,int,int)), SIGNAL(SignalWindowingValue(int,int,int,int)));
			connect(pPanel, SIGNAL(SignalHistogramData(QMap<int,int>)), SIGNAL(SignalHistogramData(QMap<int,int>)));
			connect(pPanel, SIGNAL(SignalProfileDraw(QList<int>)), SIGNAL(SignalProfileDraw(QList<int>)));
			connect(pPanel, SIGNAL(SignalDoubleClick()), SLOT(SlotItemDoubleClick()));
			connect(pPanel, SIGNAL(SignalHistory(QAction*,QAction*)), SIGNAL(SignalHistory(QAction*,QAction*)));

			m_listChild << pPanel;
			m_pLayout->addWidget(pPanel, row, column, 1, 1);
		}
	}
}

void MultiLayout::saveImage(bool bIsDiagnosis)
{
	if(m_iSelectedIndex > -1)
	{
		m_listChild.at(m_iSelectedIndex)->saveImage(bIsDiagnosis);
	}
}

void MultiLayout::SlotWindowing(int width, int center)
{
	if(m_iSelectedIndex > -1)
	{
		m_listChild.at(m_iSelectedIndex)->SlotWindowing(width, center);
	}
}

void MultiLayout::SlotGamma(int gamma)
{
	if(m_iSelectedIndex > -1)
	{
		m_listChild.at(m_iSelectedIndex)->SlotGamma(gamma);
	}
}

void MultiLayout::SlotSetTool(ETool eTool)
{
	m_eTool = eTool;

	switch (m_eTool)
	{
	case eToolReset:
		if(m_iSelectedIndex > -1) m_listChild.at(m_iSelectedIndex)->reset();
		m_eTool = eToolNone;
	default:
		foreach (IImagePanel *pPanel, m_listChild)
		{
			pPanel->SlotSetTool(m_eTool);
		}
		break;
	}
}

void MultiLayout::SlotChildSelected()
{
	QObject *pObj = this->sender();

	m_iSelectedIndex = -1;

	for (int var = 0; var < m_listChild.count(); ++var)
	{
		IImagePanel *pPanel = m_listChild.at(var);

		if((QObject*)pPanel == pObj)
		{
			m_iSelectedIndex = var;
			pPanel->setSelect(true);
		}
		else pPanel->setSelect(false);
	}
}

void MultiLayout::SlotSaveCurrent()
{
	this->saveImage(false);
}

void MultiLayout::SlotSaveDiagnosis()
{
	this->saveImage(true);
}

void MultiLayout::SlotItemDoubleClick()
{
	QObject *pObj = this->sender();

	QWidget *pWidget = qobject_cast<QWidget*>(pObj);

	if(pWidget)
	{
		if(m_posMaxWidget.x() > -1 && m_posMaxWidget.y() > -1) this->setItemNormal(pWidget);
		else this->setItemMax(pWidget);
	}
}

void MultiLayout::setItemMax(QWidget *pWidget)
{
	int index = m_pLayout->indexOf(pWidget);
	int iRow, iColumn, iTmp;

	m_pLayout->getItemPosition(index, &iRow, &iColumn, &iTmp, &iTmp);
	m_posMaxWidget.setX(iRow);
	m_posMaxWidget.setY(iColumn);

	m_pLayout->addItem(m_pLayout->takeAt(index), 0, 0, m_gridSize.width(), m_gridSize.height());
	pWidget->raise();
}

void MultiLayout::setItemNormal(QWidget *pWidget)
{
	if(m_posMaxWidget.x() > -1 && m_posMaxWidget.y() > -1)
	{
		int index = m_pLayout->indexOf(pWidget);

		m_pLayout->addItem(m_pLayout->takeAt(index), m_posMaxWidget.x(), m_posMaxWidget.y(), 1, 1);
	}
	else Q_ASSERT(false);

	m_posMaxWidget.setX(-1);
	m_posMaxWidget.setY(-1);
}
